const origin = location.origin
$(function () {


  // 监听第一行的复选框，
  $("input[name='multipleSelection']").click(function () {
    // debugger
    // 删除选中
    let isDelete = $('#isGeneratorDelete').prop('checked')
    let isSelect = $('#isGeneratorSelect').prop('checked')

    // console.log(isDelete);
    if (isDelete) {
      $('#GeneratorDeleteL').show()
    } else {
      $('#GeneratorDeleteL').hide()
    }
    if (isSelect) {
      $('.mybatisPlusConfig').show()
      $('.SelectCustom').show()
    } else {
      $('.mybatisPlusConfig').hide()
      $('.SelectCustom').hide()
    }
  });

  // 监听生成自定义查询方法
  $("input[name='isSelectCustom']").click(function () {
    let isSelectCustom = $('#isSelectCustom').prop('checked')
    let str = `<p>
      <label for="column">列名：</label>
      <input type="text" id="column" name="column" required>
      <label for="condition">条件：</label>
      <input type="text" id="condition" name="condition" required>
      <label for="value">值：</label>
      <input type="text" id="value" name="value" required>
      <span class="del">—</span>
    </p>`
    console.log(isSelectCustom);
    if (isSelectCustom) {
      $('.radioBox_item_1').show()
      $('#listData').show()
      $('.radioBox_item_3').show()

      $('#listData').empty()
      $('#listData').append(str)

    } else {
      $('.radioBox_item_1').hide()
      $('#listData').hide()
      $('.radioBox_item_3').hide()
      //
      $('#listData').html('')
      $("#isOrder").prop('checked', false)
      onclose()

    }
  });
  // 监听排序
  $("input[name='isOrder']").click(function () {
    let isOrder = $('#isOrder').prop('checked')
    if (isOrder) {
      $('.radioBox_item_2').show()
      $(".radioBox_item_2_l").show()
    } else {
      $('.radioBox_item_2').hide()
      $(".radioBox_item_2_l").hide()
      onclose()
    }
  });
  // 顺序
  $("input[name='isOrderASC']").click(function () {
    let isOrderASC = $('#isOrderASC').prop('checked')
    if (isOrderASC) {
      $('#isOrderASC_inp').show()
    } else {
      $('#isOrderASC_inp').hide()
      $("#isOrderASCValue").val('')
    }
  });
  // 逆序
  $("input[name='isOrderDESC']").click(function () {
    let isOrderDESC = $('#isOrderDESC').prop('checked')
    if (isOrderDESC) {
      $('#isOrderDESC_inp').show()
    } else {
      $('#isOrderDESC_inp').hide()
      $("#isOrderDESCValue").val('')
    }
  });

  // 生成excel导出功能
  $("input[name='isGeneratorExcelDown']").click(function () {
    let isGeneratorExcelDown = $('#isGeneratorExcelDown').prop('checked')
    let str = `<p>
      <label for="column">列名：</label>
      <input type="text" id="column" name="column" required>
      <label for="condition">条件：</label>
      <input type="text" id="condition" name="condition" required>
      <label for="value">值：</label>
      <input type="text" id="value" name="value" required>
      <span class="del">—</span>
    </p>`
    if (isGeneratorExcelDown) {
      $('#AddExcel').show()
      $('#listData1').empty()
      $('#listData1').append(str)
    } else {
      $('#AddExcel').hide()
      // $("#listData1").val('')
      $('#listData1').empty()
    }
  });
  // 清除
  function onclose() {
    // 关闭复选框
    $("#isOrderASC").prop('checked', false)
    $("#isOrderDESC").prop('checked', false)
    $("#isOrderASCValue").val('')
    $("#isOrderDESCValue").val('')

    $('.radioBox_item_2').hide()
    $(".radioBox_item_2_l").hide()
    $('#isOrderASC_inp').hide()
    $('#isOrderDESC_inp').hide()
  }
  // 添加1
  $("#AddFun").click(function () {
    var str = `
    <p>
    <label for="column">列名：</label>
    <input type="text" id="column" name="column" required>
    <label for="condition">条件：</label>
    <input type="text" id="condition" name="condition" required>
    <label for="value">值：</label>
    <input type="text" id="value" name="value" required>
    <span class="del">—</span>
  </p>

    `
    $('#listData').append(str)

  })
  $(document).on('click', '.del', function () {
    // alert($(this).text());
    $(this).closest('p').remove();
  });
  // 添加2
  $("#AddExcel").click(function () {
    var str = `
    <p>
    <label for="column">列名：</label>
    <input type="text" id="column" name="column" required>
    <label for="condition">条件：</label>
    <input type="text" id="condition" name="condition" required>
    <label for="value">值：</label>
    <input type="text" id="value" name="value" required>
    <span class="del">—</span>
  </p>

    `
    $('#listData1').append(str)

  })

  // 提交
  $("#butSend").click((e) => {
    let parms = {
      dataSourceConfig: {
        driver: $('#driver').val(),
        url: $("#url").val(),
        userName: $("#username").val(),
        password: $("#password").val(),
      }, tableConfig: {
        tableName: $("#tableName").val(), tableFilterPrefix: $("#tableFilterPrefix").val(),
      }, baseInfoConfig: {
        path: $("#path").val(),
        pack: $("#pack").val(),
        mapperXmlUrl: $("#xmlUrl").val(),
        isGeneratorSelect: $("#isGeneratorSelect").prop('checked'),
        isGeneratorSave: $("#isGeneratorSave").prop('checked'),
        isGeneratorUpdate: $("#isGeneratorUpdate").prop('checked'),
        isGeneratorDelete: $("#isGeneratorDelete").prop('checked'),
        isGeneratorR: $("#isGeneratorR").val(),
        isGeneratorMybatisPlusConfig: $("#isGeneratorMybatisPlusConfig").prop('checked'),
        isSelectCustom: $("#isSelectCustom").prop('checked'),
        isGeneratorExcelImport: $("#isGeneratorExcelImport").prop('checked'),
        isGeneratorExcelDown: $("#isGeneratorExcelDown").prop('checked'),
      }, orderBy: {
        columnsDESC: [], columnsASC: []
      }, selectCustomList: []
    }
    if (parms.baseInfoConfig.isGeneratorDelete) {
      parms.baseInfoConfig.isGeneratorDeleteL = $("#isGeneratorDeleteL").val()
    }
    if (parms.baseInfoConfig.isSelectCustom) {
      parms.baseInfoConfig.isOrder = $("#isOrder").prop('checked')
    }
    if (parms.baseInfoConfig.isOrder) {
      parms.baseInfoConfig.isOrderDESC = $("#isOrderDESC").prop('checked')
      parms.baseInfoConfig.isOrderASC = $("#isOrderASC").prop('checked')
    }
    if (parms.baseInfoConfig.isOrderASC) {
      parms.orderBy.columnsASC = $("#isOrderASCValue").val().split(',')
    }
    if (parms.baseInfoConfig.isOrderDESC) {
      parms.orderBy.columnsDESC = $("#isOrderDESCValue").val().split(',')
    }
    let list = [],list2 = []
    $('#listData p').each(function (index, item) {
      let column = $(item).find('input[name=column]').val()
      let condition = $(item).find('input[name=condition]').val()
      let columnBean = $(item).find('input[name=value]').val()
      list.push({ column, condition, columnBean })
    });
    $('#listData1 p').each(function (index, item) {
      let column = $(item).find('input[name=column]').val()
      let condition = $(item).find('input[name=condition]').val()
      let columnBean = $(item).find('input[name=value]').val()
      list2.push({ column, condition, columnBean })
    });
    // console.log(list);
    parms.selectCustomConditionList = list
    parms.selectCustomConditionExcelList = list2
    console.log(parms);
// debugger
// return
    $.ajax({
      url: origin + '/generator/code',
      data: JSON.stringify(parms),
      method: "post",
      dataType: 'json',
      contentType: 'application/json',
      success: function (data) {
        console.log(data);
      }
    })


  })

})
